package com.qzsoft.system.modules.sys.noticeTemplate.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qzsoft.jeemis.common.annotation.LogOperation;
import com.qzsoft.jeemis.common.constant.Constant;
import com.qzsoft.jeemis.common.utils.ExcelUtils;
import com.qzsoft.jeemis.common.utils.Result;
import com.qzsoft.jeemis.common.validator.AssertUtils;
import com.qzsoft.jeemis.common.validator.ValidatorUtils;
import com.qzsoft.jeemis.common.validator.group.AddGroup;
import com.qzsoft.jeemis.common.validator.group.DefaultGroup;
import com.qzsoft.jeemis.common.validator.group.UpdateGroup;
import com.qzsoft.system.modules.sys.noticeTemplate.entity.SysNoticeTemplateEntity;
import com.qzsoft.system.modules.sys.noticeTemplate.service.SysNoticeTemplateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * @author sdmq
 * @date 2019/9/17 20:52
 */
@RestController
@RequestMapping("/sys/noticetemplate")
@Api(tags="通知模板")
public class SysNoticeTemplateController {
	@Autowired
	private SysNoticeTemplateService noticeTemplateService;

	@GetMapping("page")
	@ApiOperation("分页")
	@ApiImplicitParams({
			@ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query", required = true, dataType="int") ,
			@ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query",required = true, dataType="int") ,
			@ApiImplicitParam(name = Constant.ORDER_FIELD, value = "排序字段", paramType = "query", dataType="String") ,
			@ApiImplicitParam(name = Constant.ORDER, value = "排序方式，可选值(asc、desc)", paramType = "query", dataType="String") ,
			@ApiImplicitParam(name = "title", value = "模板标题", paramType = "query", dataType="String")
	})
	@RequiresPermissions("sys:notice:page")
	public Result<IPage<SysNoticeTemplateEntity>> page(Page page, SysNoticeTemplateEntity params){
		IPage<SysNoticeTemplateEntity> dataPage = noticeTemplateService.page(page, params);

		return new Result<IPage<SysNoticeTemplateEntity>>().ok(dataPage);
	}

	@GetMapping("list")
	@ApiOperation("列表")
	@RequiresPermissions("sys:notice:list")
	public Result<List<SysNoticeTemplateEntity>> list(@ApiIgnore @RequestParam Map<String, Object> params){
		List<SysNoticeTemplateEntity> data = noticeTemplateService.list(params);

		return new Result<List<SysNoticeTemplateEntity>>().ok(data);
	}

	@GetMapping("{id}")
	@ApiOperation("信息")
	@RequiresPermissions("sys:notice:info")
	public Result<SysNoticeTemplateEntity> get(@PathVariable("id") String id){
		SysNoticeTemplateEntity data = noticeTemplateService.get(id);

		return new Result<SysNoticeTemplateEntity>().ok(data);
	}

	@PostMapping
	@ApiOperation("保存")
	@LogOperation("保存")
	@RequiresPermissions("sys:notice:save")
	public Result save(@RequestBody SysNoticeTemplateEntity dto){
		//效验数据
		ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);
		noticeTemplateService.save(dto);
		return new Result();
	}

	@PutMapping
	@ApiOperation("修改")
	@LogOperation("修改")
	@RequiresPermissions("sys:notice:update")
	public Result update(@RequestBody SysNoticeTemplateEntity dto){
		//效验数据
		ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);

		noticeTemplateService.update(dto);

		return new Result();
	}

	@PostMapping("/status")
	@ApiOperation("修改模板状态")
	@LogOperation("修改模板状态")
	@RequiresPermissions("sys:notice:update")
	public Result updateStatus(@RequestBody SysNoticeTemplateEntity dto){
		//效验数据
		ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);

		noticeTemplateService.updateStatus(dto);

		return new Result();
	}

//	@PostMapping("/publish")
//	@ApiOperation("发布")
//	@LogOperation("发布")
//	@RequiresPermissions("sys:notice:update")
//	public Result publishStart(@RequestBody String[] ids){
//		//效验数据
//		AssertUtils.isArrayEmpty(ids, "id");
//		noticeTemplateService.publishStart(ids);
//		return new Result();
//	}
//
//	@PostMapping("/stop")
//	@ApiOperation("停止发布")
//	@LogOperation("停止发布")
//	@RequiresPermissions("sys:notice:update")
//	public Result publishStop(@RequestBody String[] ids){
//		//效验数据
//		AssertUtils.isArrayEmpty(ids, "id");
//		noticeTemplateService.publishStop(ids);
//		return new Result();
//	}

	@DeleteMapping
	@ApiOperation("删除")
	@LogOperation("删除")
	@RequiresPermissions("sys:notice:delete")
	public Result delete(@RequestBody String[] ids){
		//效验数据
		AssertUtils.isArrayEmpty(ids, "id");

		noticeTemplateService.delete(ids);

		return new Result();
	}



	@GetMapping("export")
	@ApiOperation("导出")
	@LogOperation("导出")
	@RequiresPermissions("sys:notice:export")
	public void export(@ApiIgnore @RequestParam Map<String, Object> params , HttpServletResponse response) throws Exception {
		List<SysNoticeTemplateEntity> list = noticeTemplateService.list(params);
		ExcelUtils.exportExcelToTarget(response, "导出通知通告列表", list, SysNoticeTemplateEntity.class);
	}
	@GetMapping("test")
	public Result test() {
		return new Result().ok("<div> <h3 @click='change'>测试2222222 {{data.msg}}</h3> <qz-dept-tree-gen></qz-dept-tree-gen></div>");
	}


}
